package com.mingqijia.gassafety.authority.vcc.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.logging.Logger;

/**
 * VCC授权配置
 * @author Damon S.
 * @version v1.0.1
 * @date 2020年07月12日 15:45
 */
@Data
@Component
@Slf4j
@ConfigurationProperties(prefix = "vcc.oauth")
public class VccOauthConfig {
    private String server;
    private String clientId;
    private String scope;
    private String responseType;
    private String redirectUri;
    private String appCode;
    private String domain;
    private String serverIp;
    private String clientIdIp;
    private String scopeIp;
    private String responseTypeIp;
    private String redirectUriIp;
    private String appCodeIp;
    private String logout;
    private String profile;


    private static final String TEMPLATE_AUTHORIZE =
            "%s/oauth/authorize?client_id=%s&scope=%s&response_type=%s&redirect_uri=%s&state=%s";

    private static final String TEMPLATE_LOGOUT =
            "%s/login/logout?clientId=%s&logoutUrl=%s";

    public String getAuthorizeUrl(HttpServletRequest request) {
        log.info("请求url:{}",request.getRequestURL());
        log.info("判断domain:{}",domain);
        if (StringUtils.equals(domain, request.getRequestURL().toString())){
            log.info("跳转的地址:{}",String.format(TEMPLATE_AUTHORIZE, serverIp, clientIdIp, scope, responseType, redirectUriIp,clientIdIp));
            return String.format(TEMPLATE_AUTHORIZE, serverIp, clientIdIp, scope, responseType, redirectUriIp,clientIdIp);
        }
            return String.format(TEMPLATE_AUTHORIZE, server, clientId, scope, responseType, redirectUri,clientId);
    }

    public String getLogoutUrl(HttpServletRequest request) {
        log.info("退出登录时判断request12:{}",request.getRequestURL());
        log.info("判断logout:{}",logout);
        if (logout.equals(request.getRequestURL().toString()) || profile.equals(request.getRequestURL().toString())){
            log.info("跳转的地址12:{}",String.format(TEMPLATE_LOGOUT, serverIp, clientIdIp, redirectUriIp));
            return String.format(TEMPLATE_LOGOUT, serverIp, clientIdIp, redirectUriIp);
        }
        log.info("跳转的地址为:{}",String.format(TEMPLATE_LOGOUT, server, clientId, redirectUri));
        return String.format(TEMPLATE_LOGOUT, server, clientId, redirectUri);
    }
}


